import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JPanel;

import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.Player;
import edu.jhu.tcj.purfuit.Space;
import edu.jhu.tcj.purfuit.Turn;
import edu.jhu.tcj.purfuit.Turn.TurnState;

/**
 * 
 */

public class SpaceUI extends JPanel implements PropertyChangeListener {
	
	private final int spaceHeight = 45;
	private final int spaceWidth = 45;
	
	private final Space space;
	private boolean highlighted;
	private final Game game;
	private final Application application;

	public SpaceUI(Space space, Game game, Application application) {
		this.space = space;
		this.game = game;
		this.application = application;
		game.addPropertyChangeListener(Turn.TURN_STATE_UPDATE, this);
		setSize(new Dimension(spaceWidth,spaceHeight));
		setMinimumSize(new Dimension(spaceWidth,spaceHeight));
		setPreferredSize(new Dimension(spaceWidth,spaceHeight));
		addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent me) {
				if (highlighted) {
					SpaceUI.this.game.getTurn().movePlayer(SpaceUI.this.space);
				}
			}
			
		});
	}
	
	public Space getSpace() {
		return this.space;
	}
	
	public void setHighlighted(boolean highlighted) {
		this.highlighted = highlighted;
	}
	
	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		Graphics2D g = (Graphics2D) graphics;
		graphics.setColor(Color.black);
		g.fillRect(0, 0, spaceWidth, spaceHeight);
		Color color = space.getCategory() == null ? Color.orange : space.getCategory().getColor();
		
		graphics.setColor(color);
		graphics.fillRoundRect(1, 1, spaceWidth-2, spaceHeight-2, 5, 5);
		Font rollagainFont = new Font("Tahoma", Font.PLAIN, 14);
		
		if (highlighted) {
			graphics.setColor(Color.yellow);
			g.setStroke(new BasicStroke(8));
			graphics.drawRoundRect(0,0, spaceWidth, spaceHeight, 5, 5);
		}
		if (space.isHeadQuarter()) {
			graphics.setColor(Color.yellow);
//			graphics.fillRoundRect(spaceWidth/2-10, spaceHeight/2-10, 10, 10, 10, 10);
			int[] xPoints = {5,5,13,21,29,37,38};
			int[] yPoints = {40,10,30,10,30,10,40};
			graphics.fillPolygon(xPoints, yPoints, 7);
		}
		
		List<Player> players = game.getPlayers();
		int[] spacingX = {0,1,0,1};
		int[] spacingY = {0,0,1,1};
		int count =0;
		for (Player player : players) {
			
			Space playerSpace = player.getSpace();
			
			if (playerSpace == space) {
				g.setStroke(new BasicStroke(1));
				graphics.setColor(player.getColor());
				int playerSize = 20;
				graphics.fillOval(playerSize/2-10 + playerSize*spacingX[count], playerSize/2-10 + playerSize*spacingY[count], playerSize, playerSize);
				graphics.setColor(Color.black);
				graphics.drawOval(playerSize/2-10+ playerSize*spacingX[count], playerSize/2-10 + playerSize*spacingY[count], playerSize, playerSize );
				count++;
			}
		}
		count =0;
		if(space.isCenterSpace()) {
			graphics.setFont(rollagainFont);
			graphics.setColor(Color.black);
			graphics.drawString("Center",1,30);
		} else if (space.isRollAgain()) {
			graphics.setFont(rollagainFont);
			graphics.setColor(Color.black);
			graphics.drawString("Roll",13,20);
			graphics.drawString("Again",6,30);
		}
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		Object newValue = evt.getNewValue();
		if (newValue instanceof TurnState) {
			TurnState turnState = (TurnState)newValue;
			switch (turnState) {
				case WaitingToRoll:
					highlighted = false;
					break;
				case WaitingToChooseSpace:
					break;
				case WaitingToAnswerQuestion:
					highlighted = false;
					break;
				case TurnOver:
					break;
			}
			repaint();
		}
	}
	
}